package com.example.thegreatwall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.thegreatwall.entity.Product;
import com.example.thegreatwall.mapper.ProductMapper;
import com.example.thegreatwall.service.ProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    public Page<Product> listProducts(Integer pageNum, Integer pageSize, Integer productTypeId) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(productTypeId != null, Product::getProductTypeId, productTypeId)
                .eq(Product::getStatus, 1)
                .orderByDesc(Product::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    public Product getProductDetail(Integer productId) {
        return getById(productId);
    }

    @Override
    public Page<Product> getProductsByType(Integer productTypeId, Integer pageNum, Integer pageSize) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getProductTypeId, productTypeId)
                .eq(Product::getStatus, 1)
                .orderByDesc(Product::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    public Page<Product> searchProducts(String keyword, Integer pageNum, Integer pageSize) {
        Page<Product> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Product::getProductName, keyword)
                .or()
                .like(Product::getDescription, keyword)
                .eq(Product::getStatus, 1)
                .orderByDesc(Product::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    public List<Product> proname(List<Integer> pid) {
        return this.baseMapper.proname(pid);
    }


}
